package com.moba.auth;

import com.moba.constant.ConstantKey;
import com.moba.domain.UserInfo;
import com.moba.security.JwtTokenUtil;
import com.moba.security.JwtUser;
import com.moba.security.JwtUserDetailsServiceImpl;
import com.moba.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl implements AuthService {

  private AuthenticationManager authenticationManager;
  private UserDetailsService userDetailsService;
  private JwtTokenUtil jwtTokenUtil;
  @Autowired UserInfoService userInfoService;

  private String tokenHead = ConstantKey.JWT_TOKEN_HEADER;

  @Autowired
  public AuthServiceImpl(
      AuthenticationManager authenticationManager,
      JwtUserDetailsServiceImpl userDetailsService,
      JwtTokenUtil jwtTokenUtil) {
    this.authenticationManager = authenticationManager;
    this.userDetailsService = userDetailsService;
    this.jwtTokenUtil = jwtTokenUtil;
    this.userInfoService = userInfoService;
  }

  @Override
  public UserInfo register(UserInfo userToAdd) {
    //        final String username = userToAdd.getUserName();
    //        if(userDetailsService.findByUsername(username)!=null) {
    //            return null;
    //        }
    //        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    //        final String rawPassword = userToAdd.getPassword();
    //        userToAdd.setPassword(encoder.encode(rawPassword));
    //        userToAdd.setLastPasswordResetDate(new Date());
    //        userToAdd.setRoles(asList("ROLE_USER"));
    //        return userRepository.insert(userToAdd);
    return null;
  }

  @Override
  public String login(String username, String password) {
    UsernamePasswordAuthenticationToken upToken =
        new UsernamePasswordAuthenticationToken(username, password);
    // Perform the security
    final Authentication authentication = authenticationManager.authenticate(upToken);
    SecurityContextHolder.getContext().setAuthentication(authentication);

    // Reload password post-security so we can generate token
    final UserDetails userDetails = userDetailsService.loadUserByUsername(username);

    final String token = jwtTokenUtil.generateToken(userDetails);
    return token;
  }

  @Override
  public String refresh(String oldToken) {
    final String token = oldToken.substring(tokenHead.length());
    String username = jwtTokenUtil.getUsernameFromToken(token);
    JwtUser user = (JwtUser) userDetailsService.loadUserByUsername(username);
    if (jwtTokenUtil.canTokenBeRefreshed(token)) {
      return jwtTokenUtil.refreshToken(token);
    }
    return null;
  }

  @Override
  public Boolean remove(String oldToken) {
    final String token = oldToken.substring(tokenHead.length());
    return jwtTokenUtil.removeToken(token);
  }
}
